package com.suxin.socket.yinfei.pusher.sync;

import com.suxin.socket.yinfei.pusher.AbstractPusherHandler;
import com.suxin.socket.yinfei.session.SessionConnectionHolder;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author Tang
 * @classname AbstractSyncPusherHandler
 * @description [ 异步 pusher ]
 * @date 2021/8/24 21:50
 */
public abstract class AbstractSyncPusherHandler<T> extends AbstractPusherHandler<T> implements AsyncPusherHandler<T> {

    private ThreadPoolExecutor threadPoolExecutor;

    public AbstractSyncPusherHandler(SessionConnectionHolder sessionConnectionHolder) {
        super(sessionConnectionHolder);
        threadPoolExecutor = new ThreadPoolExecutor(5, 6,
                100, TimeUnit.MILLISECONDS, new LinkedBlockingDeque<>(), Executors.defaultThreadFactory());
    }

    /**
     * 异步pusher接口
     *
     * @param msg
     */
    @Override
    public void push(T msg) {
        threadPoolExecutor.execute(() -> {
            doSendMsg(msg);
        });
    }

    /**
     * doSendMsg
     *
     * @param msg
     */
    protected abstract void doSendMsg(T msg);
}